package com.huizhan.web.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.*;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import com.huizhan.web.model.*;

@Repository 
public interface SourceDao extends JpaRepository<Source,Long>{
    
    /**
     * 根据名称和项目ID获取值
     * @param registerId
     * @return
     */
    Source findByNameAndRegisterId(String name,Long registerId);
    
    /**
     * 根据名称和项目ID获取值
     * @param registerId
     * @return
     */
    Integer countByNameAndRegisterIdAndSourceIdNot(String name,Long registerId,Long sourceId);
    
    /**
     * 根据注册ID和名称
     */
     Source findByRegisterIdAndName(Long registerId,String name);
     
     /**
      * 根据注册ID获取来源
      * @param registerId
      * @return
      */
     List<Source> findByRegisterId(Long registerId);

    /**
     * 根据项目ID获取值
     * @param projectId
     * @return
     */
    List<Source> findByProjectId(String projectId);
    
    /**
     * 分页查赠票集合
     * @param name 名称
     * @param phone 手机号
     * @param project_Id 项目ID
     * @param pageable 分页参数
     * @return 票种集合
     */
    @Query(
        value = "SELECT * FROM source"
            + " WHERE (name LIKE CONCAT('%',?1,'%') OR ?1  = '')"
            + " AND (project_Id =?2 OR ?2  = '') "
            + " AND (type =?3 OR ?3 = 0 )", 
        countQuery = "SELECT COUNT(1) FROM source"
            + " WHERE (name LIKE CONCAT('%',?1,'%') OR ?1  = '')"
            + " AND (project_Id =?2 OR ?2  = '') "
            + " AND (type =?3 OR ?3 = 0 )",
        nativeQuery = true
    )
    Page<Source> findByWhere(String name,String projectId,Long type, Pageable pageable);
}